package cn.fl.splmsg.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.fl.splmsg.dto.SplBullMsgCfgDTO;
import cn.fl.splmsg.condition.SplBullMsgCfgQueryCondition;
import cn.fl.splmsg.service.ISplBullMsgCfgApiService;
import cn.fl.splmsg.service.ISplBullMsgCfgService;
import cn.fl.splmsg.transform.SplBullMsgCfgVOTransform;
import cn.fl.splmsg.vo.SplBullMsgCfgVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cmm.comm.facade.framework.api.ISysDictionaryApiService;

import java.util.List;

/**
 * @Description: 公告消息配置表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class SplBullMsgCfgServiceImpl implements ISplBullMsgCfgService {

	/**
	 * 公告消息配置表 暴露服务
	 */
	@Autowired
	private ISplBullMsgCfgApiService splBullMsgCfgApiService;
	/**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public SplBullMsgCfgVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return null;
		}
		SplBullMsgCfgDTO dto = this.splBullMsgCfgApiService.selectSplBullMsgCfgById(id);
		return SplBullMsgCfgVOTransform.toVO(dto);
	}

	@Override
	public Long add(SplBullMsgCfgVO entity) throws BaseException{
		if(null == entity){
			return null;
		}
		//TODO 此处是否需要将省、市编码带出文字
		return this.splBullMsgCfgApiService.addSplBullMsgCfg(SplBullMsgCfgVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException{
		if(CollectionUtils.isEmpty(ids)){
			return false;
		}
		return this.splBullMsgCfgApiService.removeSplBullMsgCfgByIds(ids,CurrentThreadContext.getCurrentUserId());
	}

	@Override
	public boolean modify(SplBullMsgCfgVO entity) throws BaseException{
		if(null == entity){
			return false;
		}
		return this.splBullMsgCfgApiService.modifySplBullMsgCfg(SplBullMsgCfgVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
        SplBullMsgCfgQueryCondition condition=null;
        if(null == page) {
            page = new Pager();
            page.setCondition(new SplBullMsgCfgQueryCondition());
        }
		condition=(SplBullMsgCfgQueryCondition)page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		page=this.splBullMsgCfgApiService.searchSplBullMsgCfgListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
            List<SplBullMsgCfgVO> voList=SplBullMsgCfgVOTransform.toVOList((List<SplBullMsgCfgDTO>)page.getResultList());
            //this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
	}
	
	@Override
	public List<SplBullMsgCfgVO> searchList(SplBullMsgCfgQueryCondition condition) throws BaseException{
		if(null == condition){
			condition=new SplBullMsgCfgQueryCondition();
		}
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
		condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
		List<SplBullMsgCfgVO> voList=SplBullMsgCfgVOTransform.toVOList(this.splBullMsgCfgApiService.searchSplBullMsgCfgList(condition));
		this.setDictData(voList);
		return voList;
	}
	
    /**
     * 添加数据字典名称
     * @param voList
     */
	public void setDictData(List<SplBullMsgCfgVO> voList){
		if(CollectionUtils.isEmpty(voList)){
			return;
		}
		/*Map<String, String> _CdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, SplBullMsgCfgConstant.CODE_CD);
		for(SplBullMsgCfgVO vo:voList){
			if (_CdMap != null && _CdMap.containsKey(vo.getCd())) {
            	vo.setCdNm(_CdMap.get(vo.getCd()));
        	}	
		}*/
	}
	
	/**
     * 查询数据字典
     * @param groupCd 组代码
     * @return List<KeyValueVO>
     */
	@Override
    public List<KeyValueVO> searchCdList(String groupCd){
        return this.sysDictionaryApiService.getKvList(groupCd, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

	@Override
	public Boolean addSplMsgCfgBatch(List<SplBullMsgCfgVO> splBullMsgCfgVOList,
			Long logId) throws BaseException {
		if(StringUtils.isEmpty(logId)){
			return false;
		}
		return this.splBullMsgCfgApiService.addSplBullMsgCfgBatch(SplBullMsgCfgVOTransform.toDTOList(splBullMsgCfgVOList), logId);
	}
	    
}
